var request = require('request');
var config = require('../../config');
// var accessTokenDao = require('../dao/AccessToken2RedisDao');
var accessTokenDao = require('../dao/AccessTokenDao');
var logger = require('../../common/Logger/Logger');
var accessTokenUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + config.app.appid + '&secret=' + config.app.appsecret;
var accessToken = '';
var jsapiTicketController = require('./JsapiTicketController');

var verify = require('../../common/utils/Notify').verify;


module.exports = {
    getAccessToken:function(){
        return accessToken;
    },
    getAccessTokenBySign:function (req,res){
        var sign = req.body.sign;
        var nonestr = req.body.nonestr;
        var timestamp = req.body.timestamp;

        if(sign&&nonestr){
            // if (verify(sign, 'nonestr='+nonestr+'&timestamp='+timestamp)) {
            if (verify(sign, nonestr+timestamp)) {
                accessTokenDao.getAccessToken(function (data) {
                    if (data.isSuccess) {
                        res.json({
                            status: 'success',
                            access_token: data.rows[0].access_token,
                            expires_in: data.rows[0].expires_in
                        });
                    }
                });
            }else{
                res.json({
                    status:'error',
                    message:'秘钥校验失败'
                });

            }
        }else{
            res.json({
                status:'error',
                message:'参数有误'
            });
        }

    }
};

var queryAccessTokenFromWechat = function () {
    request({
        url: accessTokenUrl,
        method: 'GET'
    }, function (err, response, body) {
        body = JSON.parse(body);
        if (err) {
            logger.log('从微信服务器请求AccessToken失败: ', err);
            return;
        }
        accessToken = body.access_token;
        console.log('AccessToken更新: ' + accessToken);
        saveAccessToken(body);
        //更新JsapiTicket
        jsapiTicketController.queryJsapiTicketFromWechat(accessToken);
        setTimeout(function () {
            queryAccessTokenFromWechat();
            //更新JsapiTicket
            jsapiTicketController.queryJsapiTicketFromWechat(accessToken);
            //每次提前十分钟去请求
        }, (body.expires_in - 600) * 1000);
    })
};

var queryAccessTokenFromDatabase = function () {
    accessTokenDao.getAccessToken(function (data) {
        if (data.isSuccess) {
            if (expired(data.rows)) {
                queryAccessTokenFromWechat();
            } else {
                accessToken = data.rows[0].access_token;
                console.log('\nAccessToken init from database: ' + accessToken + '\n\n');
                var timeRemaining = data.rows[0].expires_in - Date.now();
                //初始化JsapiTicket
                jsapiTicketController.initJsapiTicket(accessToken);

                setTimeout(function () {
                    queryAccessTokenFromWechat();
                    //每次提前十分钟去请求
                }, timeRemaining - 600000);
            }
        } else {
            console.log('从数据库请求AccessToken失败: ');
        }
    });
};

var expired = function (rows) {
    // return true;
    if (rows.length == 0) {
        //此时数据库还没有存access_token，所以判断为失效
        return true;
    } else {
        //得到负数，查询一下
        var timeRemaining = rows[0].expires_in - Date.now();
        console.log('\n距离AccessToken过期还有：' + timeRemaining + '毫秒');
        //如果数据库存的到期时间距离现在已经少于10分钟，则判断为失效
        return rows[0].expires_in - Date.now() < 600000;
    }
};

var saveAccessToken = function (data) {
    accessTokenDao.setAccessToken({
        access_token: data.access_token,
        expires_in: data.expires_in * 1000 + Date.now()
    }, function (message) {
        if (message.isSuccess) {
            console.log('save access token to database success.');
        } else {
            console.log('save access token to database failed: ', message.err);
        }
    });
};

var initAccessToken = function () {
    queryAccessTokenFromDatabase();
};

module.exports.initAccessToken = initAccessToken;

